/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.swing.JButton;
import model.Base;
import model.Document;
import model.Person;
import view.SearchView;

/**
 *
 * @author Ira
 */
public class SearchControler implements ItemListener, ActionListener {
	
	private Base model;
	private SearchView view;
	boolean allFeatures;
	
	public SearchControler(Base model, SearchView view, boolean allFeatures) {
		this.model = model;
		this.view = view;
		this.allFeatures = allFeatures;
	}

	@Override
	public void itemStateChanged(ItemEvent e) {
		String s = (String)e.getItem();
		if(s.equals("Livre")) {
			this.view.setBookView();
		}
		else if(s.equals("Journal")) {
			this.view.setNewsPaperView();
		}
		else if(s.equals("Magazine")) {
			this.view.setMagazineView();
		}
		else if(s.equals("CD")) {
			this.view.setCDView();
		}
		else if(s.equals("DVD")) {
			this.view.setDVDView();
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String name = ((JButton)e.getSource()).getName();
		if(name.equals(SearchView.BTN_RE_INIT)) {
			this.view.reInit();
		}
		else if(name.equals(SearchView.BTN_SEARCH)) {
			
			if(!this.view.isSearchingUser()) {
				Set<Document> docs = new HashSet<Document>();
				if(this.view.isSimpleSearch()) {
					this.simpleSearch(docs, this.view.getSimpleSearchText());
				}
				else {
					// recherche docs complete
					String dayDate = null, type = null, category;
					List<String> authors = null;
					int lenght = 0;
					
					
					// All
					if(this.view.getCbbCategory().equals("Toutes")) {
						category = null;
					}
					else {
						category = this.view.getCbbCategory();
					}
					
					// Livre, CD, DVD
					if(category.equals("Livre") || category.equals("CD") || category.equals("DVD")) {
						type = this.view.getType();
						lenght = this.view.getLenght();
					}
					
					// Magazine
					if(category.equals("Magazine")) {
						lenght = this.view.getLenght();
					}
					
					// Journal
					if(category.equals("Journal")) {
						dayDate = this.view.getDayDate();
					}
					try {
						docs = this.model.documentSearch(this.view.getTxtEditor(), this.view.getTxtTitle(), Short.parseShort(this.view.getTxtYear()), category, authors, type, lenght, dayDate);
					}
					catch(Exception ex) {
						docs = this.model.documentSearch(this.view.getTxtEditor(), this.view.getTxtTitle(), null, category, authors, type, lenght, dayDate);
					}
				}
				this.model.updateObservers(docs);
			}
			// User
			else {
				Set<Person> users = this.model.personSearch(this.view.getTxtNameText(), this.view.getTxtFirstName(), this.view.getTxtPseudo(), null);
				this.model.updateObservers(users);
			}
		}
	}
	
	private void simpleSearch(Set<Document> docs, String searchString) {
		if(this.view.isAuthorSearched()) {
			List<String> authors = new ArrayList<String>(Arrays.asList(searchString.split(" ")));
			docs.addAll(this.model.documentSearch(null, null, null, null, authors, null, 0, null));
		}
		if(this.view.isEditorSearched()) {
			docs.addAll(this.model.documentSearch(searchString, null, null, null, null, null, 0, null));
		}
		if(this.view.isYearSearched()) {
			try {
				docs.addAll(this.model.documentSearch(null, null, Short.valueOf(searchString), null, null, null, 0, null));
			}
			catch (NumberFormatException ex) {

			}
		}
		if(this.view.isTitleSearched()) {
			docs.addAll(this.model.documentSearch(null, searchString, null, null, null, null, 0, null));
		}
	}
}
